package com.example.myimageshare.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myimageshare.Main2Activity;
import com.example.myimageshare.R;
import com.example.myimageshare.View.MainActivity;
import com.example.myimageshare.entity.User;

import java.io.ByteArrayOutputStream;
import java.util.List;

public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder> {

    private List<User> users;
    private Context context;
    private int resourceId;
    private int position;


    public HomeAdapter(List<User> users){
        this.users = users;
    }

    public HomeAdapter(Context context, int resourceId, List<User> users){
        this.context = context;
        this.resourceId = resourceId;
        this.users = users;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(
                resourceId,parent,false);
        final ViewHolder viewHolder = new ViewHolder(view);

        viewHolder.iv_art.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, Main2Activity.class);
                intent.putExtra("imgUri",viewHolder.getItemId());
                context.startActivity(intent);

            }
        });
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        User user = users.get(position);
        this.position = position;
        if(user.getUserName() != null){
           holder.tv_name.setText(user.getUserName());
        }
    }

    @Override
    public int getItemCount() {
        return users.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView iv_art;
        TextView tv_name;


        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_art = itemView.findViewById(R.id.iv_image);
            tv_name = itemView.findViewById(R.id.tv_title);

        }


    }
}
